import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
// 首先声明一个状态 state
const state = {
msg: '',
counts:0,
money:600,
price:600,
heji:0,
flagzz:false,
imgs:'',
product:''
}
// 然后给 actions 注册一个事件处理函数，当这个函数被触发时，将状态提交到 mutaions中处理
const actions = {
saveName({commit}, msg) {
 commit('saveMsg', msg) // 提交到mutations中处理 
}
}
// 更新状态
const mutations = {
	
 saveMsg(state, msg) {
 state.msg = msg;
 state.flagzz=true;
 state.imgs=msg.productInfo.imgList[0]
 state.product=msg.productInfo.title
 state.counts=1
},
 adds(state,n){
        state.counts+=n;
        state.money=state.counts*state.price
    },
  reduces(state){
  	if(state.counts>0){
  		state.counts--;
  	}else{
  		state.counts=0
  	}
        if(state.money>0){
        state.money=state.money-state.price
//      state.heji=state.counts*state.price
        }else{
        	state.money=0
        }
        
   },
  heji(state){
  	 state.heji=state.counts*state.price
  },
  dele(state){
  	state.money=0
  }
}
// 获取状态信息
const getter = {
showState(state) {
 console.log(state.msg)
}
}
 
 
// 下面这个相当关键了，所有模块，记住是所有，注册才能使用
export default new Vuex.Store({
state,
getter,
mutations,
})